home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / THINKC / 4_0 / UUPC3 / MAC_SPEC / ULIB.C < prev    next >
Text File  |  1992-02-10  |  8KB  |  394 lines

  1. /*        ulib.c
  2.  
  3.  
  4.         macintosh library
  5.         
  6.  
  7.     Things to do in uu host
  8.  
  9.         serial I/O
  10.         
  11.         directory stuff
  12.             opendir, readdir, closedir
  13.  
  14.         prolog and epilog
  15.  
  16.         system call
  17.  
  18.             Portions Copyright ⌐ David Platt, 1992, 1991.  All Rights Reserved
  19.             Worldwide.
  20.  
  21. */
  22.  
  23. #include <stdio.h>
  24. #include "dcp.h"
  25. #ifdef     THINK_C
  26. #   include <unix.h>
  27. #else
  28. #    include <sgtty.h>
  29. #endif
  30.  
  31. /*
  32.  *
  33.  *      login (for slave in SLAVELOG mode)
  34.  * Real dumb login handshake.
  35.  *     waitforever = TRUE if an unlimited number of login attempts should be
  36.  *                   permitted (slave mode), and FALSE if the login process
  37.  *                   should terminate after a few attempts (auto mode).
  38.  *     callup_done = TRUE if some form of inbound-call preparation has been done
  39.  *                   (either via the HAYES dialer or via a DIR chat-script) and
  40.  *                   we have some confidence that there's really a caller out there.
  41.  *                   FALSE if we have no such confidence (usually because there was
  42.  *                   no INBOUND entry in the Systems file, and we've simply opened
  43.  *                   the serial port) and we want to require a carriage return to
  44.  *                   elicit the login prompt.
  45. */
  46.  
  47. login(int waitforever, int callup_done) {
  48.     char logmsg[132];
  49.     int len;
  50.     int retries;
  51. #ifdef SLAVELOG
  52.     msgtime = 1;
  53.     retries = 5;
  54.     while (callup_done && --retries >= 0 && rmsg(logmsg, 0) != 0) {
  55.         ; /* flush any dross in the input queue */
  56.     }
  57.     retries = 10;
  58.     do {
  59.         msgtime = 2 * MSGTIME;
  60.         if (!callup_done) {
  61.             if (0 == rmsg(logmsg, 0)) goto next;
  62.         }
  63.         sprintf(logmsg, "\r\n\n%s uupc listener\r\n\nUsername: ", nodename);
  64.         wmsg(logmsg, 0);
  65.         if (0 == rmsg(logmsg, 0)) goto next;
  66.         if (strstr(logmsg, "Username:") || strstr(logmsg, "Password:") || strstr(logmsg, "NO CARRIER")) {
  67.             printmsg(0, "Modem hung up");
  68.             retries = 0;
  69.             goto next;
  70.         }
  71.         printmsg( 0, "Username = %s", logmsg );
  72.         wmsg("\r\nPassword: ", 0);
  73.         if (0 == rmsg(logmsg, 0)) goto next;
  74.         printmsg( 14, "\r\nPassword = %s", logmsg );
  75.         if(strcmp(logmsg, password) == EQUAL) 
  76.             return 'I';
  77.         wmsg("\r\nSorry, incorrect login\r\n", 0);
  78. next: ;
  79.     } while ((waitforever || --retries > 0) && Main_State == Call_Systems);
  80. #endif SLAVELOG
  81.     return('A');
  82. }
  83.  
  84.  
  85. char *inbuf;
  86. char *outbuf;
  87.  
  88. swrite(data, num)
  89.     char *data;
  90.     int    num; {
  91.  
  92.     int test;
  93.     unsigned char * cp;
  94.  
  95.     if (debuglevel > 14)
  96.         fputc( '{', stderr );
  97.     if (debuglevel > 14) {
  98.         test = num;
  99.         cp = (unsigned char *)data;
  100.         while (test--)
  101.             fprintf( stderr, isprint(*cp)? "{%c}":"{%02x}", *cp++ );
  102.     }
  103.     test = SIOWrite( data, num );
  104.     if (debuglevel > 14)
  105.         fputc( '}', stderr );
  106.     return( test );
  107.  
  108. }
  109.  
  110. void makenull(int refno)
  111. {
  112.     lseek(refno, 0L, SEEK_SET);
  113.     SetEOF(refno, 0L);
  114. }
  115.  
  116. /* non-blocking read essential to "g" protocol */
  117. /* see "dcpgpkt.c" for description */
  118. /* This all changes in a mtask systems. Requests for */
  119. /* I/O should get qued and an event flag given. Then the */
  120. /* requesting process (e.g.gmachine()) waits for the event */
  121. /* flag to fire processing either a read or a write. */
  122. /* Could be implemented on VAX/VMS or DG but not MS-DOS */
  123. sread (buf, num, timeout)
  124.     char    *buf;
  125.     int    num;
  126.     long int timeout; {
  127. /*
  128.     return( SIORead( buf, num, num, timeout*10 ) );
  129. */
  130.     int count;
  131.     int test;
  132.     unsigned char * cp;
  133.  
  134.     if (debuglevel > 13)
  135.         fputc( '[', stderr );
  136.     printmsg( 15, "sread: num: %d  timeout: %d", num, timeout );
  137.         
  138.     count = SIORead( buf, num, num, timeout*10 );
  139.     printmsg( 15, "sread: read: %d ", count );
  140.  
  141.     if (debuglevel > 13 && count > 0) {
  142.         test = count;
  143.         cp = (unsigned char *)buf;
  144.         while (test--)
  145.             fprintf( stderr, isprint(*cp)? "[%c]":"[%02x]", *cp++ );
  146.     }
  147.         
  148.     if (debuglevel > 13)
  149.         fputc( ']', stderr );
  150.     return( count );    
  151.     
  152. }
  153.  
  154. /* check number of chars in input buffer */
  155.  
  156. int savail()
  157. {
  158.     return SIOAvail();
  159. }
  160.  
  161.  
  162.  
  163. openline(name, baud)
  164.     char *name, *baud; {
  165.  
  166.     printmsg(3, "openline: name: \"%s\"  baud: \"%s\"", name, baud);
  167.     SIOInit(name, baud );
  168.     inbuf = NewPtr(PORTBUFSIZ);
  169.     if (inbuf) {
  170.         SIOInBuffer(inbuf, PORTBUFSIZ);
  171.     }
  172.     outbuf = NewPtr(PORTBUFSIZ);
  173.     if (outbuf) {
  174.         SIOOutBuffer(outbuf, PORTBUFSIZ);
  175.     }
  176.     return(0);
  177. }
  178.  
  179.  
  180. closeline()
  181. {
  182.     SIOClose( 1 );
  183.     if (inbuf) DisposPtr(inbuf);
  184.     if (outbuf) DisposPtr(outbuf);
  185. }
  186.  
  187. flowcontrol(int status)
  188. {
  189.     SIOSetFlowCtl(status);
  190. }
  191.  
  192. nodot(string)
  193. {
  194. }
  195.  
  196.  
  197.  
  198. notimp( argc, argv )
  199. char *argv[];
  200. {
  201.     /*debuglevelMsg("\Pcheck argc (08) and argv (0a) ");*/
  202.     fprintf( stderr, "shell: %s not implemented\n", *argv );
  203. }
  204.  
  205. /*         shell
  206.  
  207.  
  208. */
  209.  
  210. char *getcwd();
  211.  
  212. shell(command, inname, outname)
  213.     char *command;
  214.     char *inname;
  215.     char *outname; {
  216.  
  217.     char *argvec[50];
  218.     int is_ok;
  219.  
  220.     int rnews();
  221.     int macbin();
  222.     
  223.     int argcp;
  224.  
  225.     char **argvp;
  226.     char args;
  227.     char *openFlags;
  228.     
  229.  
  230.     int    (*proto)();
  231.     
  232.     argcp = 0;
  233.  
  234.     argcp = getargs( command, argvec );
  235.  
  236.     argvp = argvec;
  237.     args = argcp;
  238.  
  239.     if ( debuglevel > 5 ) {
  240.         while ( args ) 
  241.             fprintf( stderr, "arg: %d  %s\n", args--, *argvp++ );
  242.         argvp = argvec;
  243.         args = argcp;
  244.     }
  245.     /* */
  246.     
  247.     proto = notimp;
  248.     openFlags = "r";
  249.  
  250.     if ( strcmp( *argvp, "rmail" ) == SAME )
  251.         proto = rmail;
  252.  
  253.     else if ( strcmp( *argvp, "rnews" ) == SAME ) {
  254.         openFlags = "rb";
  255.         proto = rnews;
  256.     }
  257.     
  258.     else if ( strcmp( *argvp, "macbin" ) == SAME ) {
  259.         openFlags = "rb";
  260.         proto = macbin;
  261.     }
  262.     
  263.         
  264.     if ( *inname != '\0' ) {
  265.         /* fprintf( stderr, "reopening stdin as %s\n", inname );
  266.         fprintf( stderr, "curdir: %s\n", getcwd(NULL, 0)); /* */
  267.         if ( freopen( inname, openFlags, stdin ) == NULL ) {
  268.             fprintf( stderr, "Couldn't open %s, %d\n", inname, errno );
  269.         }
  270.     }
  271.     is_ok = (*proto)( argcp, argvp );
  272.  
  273.     if (freopen( ".console", "r", stdin ) == NULL) {;
  274.         fprintf( stderr, "Couldn't reopen console, %d\n", errno );
  275.     }
  276.     return is_ok;
  277. }
  278.  
  279.  
  280. /*
  281.     macbin
  282.  
  283.     copy a macbinary image from stdin into a macintosh file
  284.  
  285.         128 bytes of header information
  286.         (n+128)/128*128 bytes of data fork data
  287.         (n+128)/128*128 bytes of resource fork data
  288.  
  289.     The name is derived from the header information
  290.  
  291.     The directory is given as an arguement
  292.  
  293.  
  294.     on remote system use:
  295.  
  296.         uux macfile.mb > remotemac!macbin /usr/sl
  297.  
  298.  
  299. */
  300.  
  301. #undef TRUE
  302.  
  303. #ifndef    THINK_C
  304. # include <types.h>
  305. # include <pb.h>
  306. #endif
  307.  
  308. /* #include <stdio.h> */
  309.  
  310. typedef struct {
  311.     /*
  312.     char                blknum;
  313.     char                mblknum;
  314.     */
  315.     char                dummy;
  316.  
  317.     char                version;
  318.  
  319.     char                fName[64];
  320.     FInfo                finderInfo;            /* check - starts on odd byte boundary */
  321.     char                protected;
  322.     char                ckZero1;
  323.     long                dfLength;
  324.     long                rfLength;
  325.     long                crDat;
  326.     long                mdDat;
  327.     char                fill1[27];
  328.     char                computer;
  329.     char                os;
  330.     } MacBinHeader;
  331.  
  332.  
  333.  
  334. int macbin( argc, argv )
  335. int argc;
  336. char *argv[];
  337. {
  338. #ifdef TESTETEST
  339.     char    filename[132];
  340.     char    buf[BUFSIZ];
  341.     MacBinHeader mbh;
  342.     long    count, size;
  343.     int        i;
  344.  
  345.     HPrmBlkRec    pbr;
  346.  
  347.     /* get the header */
  348.     fread( &mbh.version, 1, 128, stdin );
  349.  
  350.  
  351.     /* create and open the file */
  352.     strncpy( pbr.ioNamePtr, mbh.fName, 64 );
  353.     pbr.ioVRefNum = 0;
  354.     pbr.u.hfp.ioFVersNum = 0;
  355.     PBCreate( &pbr, FALSE );
  356.  
  357.  
  358.     /* get finder info */
  359.     pbr.u.hfp.ioFDirIndex = 0;
  360.     pbr.u.hfp.ioDirID = 0L;
  361.     PBGetInfo( &pbr, FALSE );
  362.     
  363.     /* set finder info */
  364.     pbr.u.hfp.ioFlFndrInfo.fdFldr = 0;
  365.     pbr.u.hfp.ioFlCrDat = mbh.crDat;
  366.     pbr.u.hfp.ioFlMdDat = mbh.mdDat;
  367.     pbr.u.hfp.ioFlFndrInfo = mbh.finderInfo;
  368.     PBSetInfo( &pbr, FALSE );
  369.  
  370.     /* get and write out the fork */
  371.     pbr.u.iop.ioPermssn = 0;
  372.     pbr.u.iop.ioMisc = 0L;
  373.     for ( i = 1; i > 0 ; i-- ) {
  374.         if (i == 1 ) {
  375.             PBOpen( &pbr, FALSE );
  376.             size = (mbh.dfLength + 127) & 0xffffff80;
  377.         }
  378.         else {
  379.             PBROpen( &pbr, FALSE );
  380.             size = (mbh.rfLength + 127) & 0xffffff80;
  381.         }
  382.         while ( size > 0 )
  383.             if ( (count = fread( buf, 1, BUFSIZ, stdin )) != 0 ) {
  384.                 size -= count;
  385.                 pbr.u.iop.ioBuffer = buf;
  386.                 pbr.u.iop.ioReqCount = count;
  387.                 PBWrite ( &pbr, FALSE);
  388.             }
  389.         PBClose( &pbr, FALSE );
  390.     }
  391. #endif
  392.     return 1;
  393. }
  394.